package com.ytjj.qmyx.admin.controller.data;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.model.request.dataStatistics.DataStatisticsRequest;
import com.ytjj.qmyx.admin.service.data.PlatformStatisticService;
import com.ytjj.qmyx.admin.utils.DateUtils;
import com.ytjj.qmyx.admin.utils.UserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description 渠道统计
 * @ClassName ChannelStatisticController
 *
 *
 * @Version 1.0
 **/

@RestController
@RequestMapping("/channelStatistics")
public class ChannelStatisticController {

    @Resource
    private PlatformStatisticService platformStatisticService;


    /**
     * 获取渠道统计列表
     *
     * @param dataStatisticsRequest
     * @return
     */
    @PostMapping("/getChannelStatisticsList")
    public CommonResult getChannelStatisticsList(@RequestBody DataStatisticsRequest dataStatisticsRequest) {

        if (StringUtils.isNotBlank(dataStatisticsRequest.getSearchDate()) && StringUtils.isNotBlank(dataStatisticsRequest.getSearchEndDate())) {
            DateTime beginDate = DateUtil.parse(dataStatisticsRequest.getSearchDate(), "yyyy-MM-dd");
            DateTime endDate = DateUtil.parse(dataStatisticsRequest.getSearchEndDate(), "yyyy-MM-dd");
            long between = DateUtil.between(beginDate, endDate, DateUnit.DAY);
            if (between > 31) {
                throw new ApiException("查询时间跨度不能超过31天");
            }
        }
        return CommonResult.success(platformStatisticService.getPlatformStatisticsListV3(dataStatisticsRequest));
    }

    /**
     * 获取渠道统计列表
     *
     * @param dataStatisticsRequest
     * @return
     */
    @PostMapping("/getChannelStatisticsListNew")
    public CommonResult getChannelStatisticsListNew(@RequestBody DataStatisticsRequest dataStatisticsRequest) {
        dataStatisticsRequest.setIsCost(1);
        Integer adminId = UserUtil.getAdminId();
        dataStatisticsRequest.setAdminId(adminId);
        if (StringUtils.isNotBlank(dataStatisticsRequest.getSearchDate()) && StringUtils.isNotBlank(dataStatisticsRequest.getSearchEndDate())) {
            DateTime beginDate = DateUtil.parse(dataStatisticsRequest.getSearchDate(), "yyyy-MM-dd");
            DateTime endDate = DateUtil.parse(dataStatisticsRequest.getSearchEndDate(), "yyyy-MM-dd");
            long between = DateUtil.between(beginDate, endDate, DateUnit.DAY);
            if (between > 31) {
                throw new ApiException("查询时间跨度不能超过31天");
            }
        }
        return CommonResult.success(platformStatisticService.getPlatformStatisticsListV2(dataStatisticsRequest));
    }

    @PostMapping("/getChannelStatisticsListV3")
    public CommonResult getChannelStatisticsListV3(@RequestBody DataStatisticsRequest dataStatisticsRequest) {

        if (StringUtils.isNotBlank(dataStatisticsRequest.getSearchDate()) && StringUtils.isNotBlank(dataStatisticsRequest.getSearchEndDate())) {
            DateTime beginDate = DateUtil.parse(dataStatisticsRequest.getSearchDate(), "yyyy-MM-dd");
            DateTime endDate = DateUtil.parse(dataStatisticsRequest.getSearchEndDate(), "yyyy-MM-dd");
            long between = DateUtil.between(beginDate, endDate, DateUnit.DAY);
            if (between > 31) {
                throw new ApiException("查询时间跨度不能超过31天");
            }
        }
        return CommonResult.success(platformStatisticService.getPlatformStatisticsList(dataStatisticsRequest));
    }
}